#!/bin/bash

# ----------------------------------------------------------------------
# Filename:   mysql_innodb.sh
# Version:    1.0
# Date:       2013/11/28
# Author:     huijing.hei
# Email:      huijing.hei@cs2c.com.cn
# Summary:    02系统基本功能-04网络服务-03Mysql-04InnoDB插件-01查看InnoDB插件
# Notes:       ***
# Copyright:    China Standard Software Co., Ltd.
# History：     
#             Version 1.0, 2013/11/27
#             - Start mysqld service.
#             - Create database/table, and add/delete/update/select records.
# ----------------------------------------------------------------------

##! @TODO: start mysqld service
##! @AUTHOR: huijing.hei
##! @VERSION: 1.0 
##! @OUT: 0 => success; 1 => failure
function do_setup()
{
    trap do_cleanup INT EXIT

    serv=mysqld

CheckServiceExists ${serv}

if [ $? -ne 0 ];then
    echo "mysqld.service or mysqld not exists"
    CheckServiceExists mariadb
    EchoResult "mariadb.service or mariadb exists"
    serv=mariadb
fi


    RestartDaemon ${serv}

    # check start 
    StatusDaemon ${serv}
    EchoResult "service ${serv} restart"

}

##! @TODO: test base functions of mysql
##! @AUTHOR: huijing.hei
##! @VERSION: 1.0 
##! @OUT: 0 => success; 1 => failure
function do_test()
{
    TEST_USER=root
    TEST_USER_PASSWORD=""

    local INDEX=""
    if [ "${serv}" == "mysqld" ];then
        INDEX="mysql>"
    elif [ "${serv}" == "mariadb" ];then
        INDEX="MariaDB"
    fi


    expect <<-EOF
        set timeout 20
        spawn mysql -u${TEST_USER} -p
            expect "Enter password:"
            send "${TEST_USER_PASSWORD}\r"

            # login
            expect {
                "${INDEX}" { send "show plugins;\r" }
                "ERROR 1045 (28000): Access denied" { send_user "Access denied\r"; exit 1; }
                "ERROR 2002 (HY000): Can't connect to local MySQL server through socket" { send_user "mysqld service not start, pls check\r"; exit 1; }
            }

            # check database mysql
            expect "rows in set" { send_user "PASS: show plugins\n"  }

            # quit
            expect "${INDEX}" { send "quit;\r" } 

EOF


}

##! @TODO: cleanup
##! @AUTHOR: huijing.hei
##! @VERSION: 1.0 
##! @OUT: 0 => success; 1 => failure
function do_cleanup()
{

    StopDaemon ${serv}
    EchoResult "stop ${serv}"

}


# include lib files

if [ -z "$SFROOT" ]
then
    echo "SFROOT is null, pls check"
    exit 1
fi

. ${SFROOT}/lib/Echo.sh
. ${SFROOT}/lib/NetOps.sh

do_setup
do_test 
EchoResult "testcase: ${serv} show innodb"
echo ""
